package cn.edu.jnu.booklistmainactivity.data;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.io.Serializable;
import java.util.Comparator;

import cn.edu.jnu.booklistmainactivity.Bookshelf;

public class BookItem implements Serializable{
    private String bookName;//书本封面名
    private int imageId;//书本图片
    private String publishyear;//出版时间
    private String publishmonth;//出版时间
    private String writerName;//作者名
    private String translator;//翻译者
    private String publishingHouse;//出版社
    private String isbn;

    private String readingStatus;//阅读状态
    private String note;
    private String website;
    private Bookshelf book_of_shelf;//存储除了all书架以外另外那个书架

    public Bookshelf getBook_of_shelf() {
        return book_of_shelf;
    }

    public void setBook_of_shelf(Bookshelf book_of_shelf) {
        this.book_of_shelf = book_of_shelf;
    }
    public BookItem(){;}
    public BookItem(String name){
        this.bookName=name;//测试的
    }

    public BookItem(String bookName, int imageId, String translator, String publishyear, String publishmonth, String writerName, String publishingHouse, String isbn){//10.19写的
        this.bookName=bookName;
        this.imageId=imageId;
        this.translator=translator;
        this.publishyear=publishyear;
        this.publishmonth=publishmonth;
        this.publishingHouse=publishingHouse;
        this.writerName=writerName;
        this.isbn=isbn;
    }
    public void setBook(BookItem book){//10.19写的
        this.bookName=book.bookName;
        this.imageId=book.imageId;
        this.translator=book.translator;
        this.publishyear=book.publishyear;
        this.publishmonth=book.publishmonth;
        this.publishingHouse=book.publishingHouse;
        this.writerName=book.writerName;
        this.isbn=book.isbn;
    }
    //点击'生成'->getter and setter就会自动生成了
    public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getImageId() {
        return imageId;
    }
    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getPublishyear() {
        return publishyear;
    }
    public void setPublishyear(String publishyear) {
        this.publishyear = publishyear;
    }

    public String getPublishmonth() {
        return publishmonth;
    }
    public void setPublishmonth(String publishmonth) {
        this.publishmonth = publishmonth;
    }

    public String getWriterName() {
        return writerName;
    }
    public void setWriterName(String writerName) {
        this.writerName = writerName;
    }

    public String getTranslator() {return translator;}
    public void setTranslator(String translator) {this.translator = translator;}

    public String getPublishingHouse() {
        return publishingHouse;
    }
    public void setPublishingHouse(String publishingHouse) {this.publishingHouse = publishingHouse;}

    public String getIsbn() {return isbn;}
    public void setIsbn(String isbn) {this.isbn = isbn;}

    public String getReadingStatus() {
        return readingStatus;
    }
    public void setReadingStatus(String readingStatus) {
        this.readingStatus = readingStatus;
    }//add页面的spinner需要用到

    public String getNote() {return note;}
    public void setNote(String note) {this.note = note;}

    public String getWebsite() {return website;}
    public void setWebsite(String website) {this.website = website;}


    public static class BookNameComparator implements Comparator<BookItem>{
        @Override
        public int compare(BookItem book1, BookItem book2) {
            try {
                String bookname1= PinyinHelper.convertToPinyinString(book1.getBookName(), "", PinyinFormat.WITHOUT_TONE);
                String bookname2= PinyinHelper.convertToPinyinString(book2.getBookName(), "", PinyinFormat.WITHOUT_TONE);
                return bookname1.compareTo(bookname2);
            } catch (PinyinException e) {
                e.printStackTrace();
            }
            return 0;
        }
    }
    public static class WriterNameComparator implements Comparator<BookItem> {
        @Override
        public int compare(BookItem book1, BookItem book2) {
            if((book1.getWriterName()==null||book1.getWriterName().length()==0)||(book2.getWriterName())==null||book2.getWriterName().length()==0){
                return 0;
            }
            try {
                String writername1= PinyinHelper.convertToPinyinString(book1.getWriterName(), "", PinyinFormat.WITHOUT_TONE);
                String writername2= PinyinHelper.convertToPinyinString(book2.getWriterName(), "", PinyinFormat.WITHOUT_TONE);
                return writername1.compareTo(writername2);
            } catch (PinyinException e) {
                e.printStackTrace();
            }
            return 0;
        }
    }
    public static class PublishHouseComparator implements Comparator<BookItem>{
        @Override
        public int compare(BookItem book1,BookItem book2) {
            if((book1.getPublishingHouse()==null||book1.getPublishingHouse().length()==0)||(book2.getPublishingHouse()==null||book2.getPublishingHouse().length()==0)){
                return 0;
            }
            try {
                String publishhouse1= PinyinHelper.convertToPinyinString(book1.getPublishingHouse(), "", PinyinFormat.WITHOUT_TONE);
                String publishhouse2= PinyinHelper.convertToPinyinString(book2.getPublishingHouse(), "", PinyinFormat.WITHOUT_TONE);
                return publishhouse1.compareTo(publishhouse2);
            } catch (PinyinException e) {
                e.printStackTrace();
            }
            return 0;
        }
    }
    public static class PubTimeComparator implements Comparator<BookItem>{
        @Override
        public int compare(BookItem book1, BookItem book2) {
            if((book1.getPublishyear()==null||book1.getPublishyear().length()==0)||(book2.getPublishyear()==null||book2.getPublishyear().length()==0)){
                return 0;
            }
            else{
                if((book2.getPublishmonth()==null||book2.getPublishmonth().length()==0)||(book2.getPublishmonth()==null||book2.getPublishmonth().length()==0)){
                    return 0;
                }
            }
            int year1=Integer.parseInt(book1.getPublishyear());
            int year2=Integer.parseInt(book2.getPublishyear());
            int month1=Integer.parseInt(book1.getPublishmonth());
            int month2=Integer.parseInt(book2.getPublishmonth());
            if(year1<year2) {
                return -1;
            }
            else if(year1>year2) {
                return 1;
            }
            else if(year1==year2) {
                if(month1<month2) {
                    return -1;
                }
                else if(month1>month2) {
                    return 1;
                }
            }
            return 0;
        }
    }

}
